package com.iceberg.database;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.iceberg.database.dictionary.DictionaryDao;
import com.iceberg.database.dictionary.DictionaryPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.concurrent.CountDownLatch;

@Slf4j
@SpringBootApplication
public class DatabaseStudyApplication implements CommandLineRunner {

    @Autowired
    private DictionaryDao dictionaryDao;

    private static CountDownLatch countDownLatch1 = new CountDownLatch(1);

    private static CountDownLatch countDownLatch2 = new CountDownLatch(1);

    @Override
    public void run(String... args) throws Exception {
        DictionaryPO dictionaryPO = this.generateDictionaryPO();
        dictionaryPO.setUrid("aaaa");
        dictionaryDao.insert(dictionaryPO);
        new Thread(() -> {
            TransactionUtil.doInTransactionRequired(() -> {
                try {
                    //Mysql数据库dictionaryPO1和dictionaryPO2是相同的，都是修改前的值
                    //Oracle数据库dictionaryPO2会变成修改后的值
                    DictionaryPO dictionaryPO1 = dictionaryDao.getById("aaaa");
                    log.warn(dictionaryPO1.toString());
                    countDownLatch1.countDown();
                    countDownLatch2.await();
                    DictionaryPO dictionaryPO2 = dictionaryDao.getById("aaaa");
                    log.warn(ObjectUtil.defaultIfNull(dictionaryPO2, new DictionaryPO()).toString());
                } catch (Exception ex) {
                    log.error("", ex);
                }
                return null;
            });
        }).start();

        new Thread(() -> {
            try {
                countDownLatch1.await();
            } catch (InterruptedException e) {
                //ignore
            }
            TransactionUtil.doInTransactionRequired(() -> {
                try {
                    //修改
//                    DictionaryPO tmp = new DictionaryPO();
//                    tmp.setUrid("aaaa");
//                    tmp.setCode(RandomUtil.randomString(6));
//                    tmp.setName(RandomUtil.randomString(6));
//                    tmp.setSysName("base");
//                    tmp.setOrderNum(RandomUtil.randomLong(99));
//                    tmp.setValue(String.valueOf(RandomUtil.randomInt(99)));
//                    dictionaryDao.update(tmp);
                    //删除
                    dictionaryDao.delete("aaaa");
                } catch (Exception ex) {
                    log.error("", ex);
                }
                return null;
            });
            countDownLatch2.countDown();
        }).start();
    }

    private DictionaryPO generateDictionaryPO() {
        DictionaryPO dictionaryPO = new DictionaryPO();
        dictionaryPO.setUrid(IdUtil.fastSimpleUUID());
        dictionaryPO.setCode(RandomUtil.randomString(6));
        dictionaryPO.setName(RandomUtil.randomString(6));
        dictionaryPO.setSysName("base");
        dictionaryPO.setOrderNum(RandomUtil.randomLong(99));
        dictionaryPO.setValue(String.valueOf(RandomUtil.randomInt(99)));
        return dictionaryPO;
    }

    public static void main(String[] args) {
        SpringApplication.run(DatabaseStudyApplication.class, args);
    }
}
